"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _configure = require("./configure");

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

// 获取默认配置
var defaultConfig = (0, _configure.getConfigure)(); // 固定前缀，以区分不同项目

var keyPrefix = function keyPrefix() {
  return defaultConfig.deployDirectory !== '/' ? defaultConfig.deployDirectory : '';
}; // 生成特使key值,不同页面、同一个页面多个


var specialKey = function specialKey(key) {
  var pathname = window.location.pathname;
  var userId = localStorage.getItem("".concat(keyPrefix(), "userId")) || '';
  return "".concat(pathname, "-").concat(userId, "-").concat(key);
};

var setItem = function setItem(key, val) {
  var isSpecial = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var tempKey = isSpecial ? specialKey(key) : keyPrefix() + key;
  var tempVal = _typeof(val) === 'object' ? JSON.stringify(val) : val;
  localStorage.setItem(tempKey, tempVal);
};

var getItem = function getItem(key) {
  var isSpecial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var needParse = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var tempKey = isSpecial ? specialKey(key) : keyPrefix() + key;
  var res = localStorage.getItem(tempKey);

  if (needParse) {
    try {
      res = JSON.parse(res);
    } catch (error) {
      // eslint-disable-next-line no-console
      console.log(error);
    }
  }

  return res;
};

var removeItem = function removeItem(key, isSpecial) {
  localStorage.removeItem(isSpecial ? specialKey(key) : keyPrefix() + key);
};

var _default = {
  setItem: setItem,
  getItem: getItem,
  removeItem: removeItem
};
exports.default = _default;